package com.bdjbd.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;

/**
 * @author ruihong
 * @date 2019/10/31
 */
public class DateUtils {
    
    public static final String FULLDATE = "yyyyMMdd";
    public static final String FULL_DATE = "yyyy-MM-dd";
    public static final String YEAR_MONTH = "yyyy-MM";
    public static final String FULL_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String FULLDATETIME = "yyyyMMddHHmmss";
    public static final String ZONE_SHANGHAI = "Asia/Shanghai";
    
    
    /**
     * 获取系统当前时间
     * @return
     */
    public static Date getCurrentTime(){
        LocalDateTime ldt = LocalDateTime.now();
        // ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = ldt.atZone(ZoneId.of(ZONE_SHANGHAI));
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    public static String getYearMonth() {
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        return String.valueOf(year) + (month < 10 ? "0"+ String.valueOf(month) : String.valueOf(month));
    }
    
    public static String getNextYearMonth() {
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        final int december = 12;
        if(month == december) {
            year++;
            month = 1;
        }else {
            month++;
        }
        return String.valueOf(year) + (month < 10 ? "0"+ String.valueOf(month) : String.valueOf(month));
    }
    
    public static String getPreYearMonth() {
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        int month = now.getMonthValue();
        final int january = 1;
        if(month == january) {
            --year;
            month = 12;
        }else {
            --month;
        }
        return String.valueOf(year) + (month < 10 ? "0"+ String.valueOf(month) : String.valueOf(month));
    }
    
    public static Integer getYear(String yearMonth) {
        return Integer.valueOf(yearMonth.substring(0, 4));
    }
    
    public static Integer getMonth(String yearMonth) {
        return Integer.valueOf(yearMonth.substring(4));
    }
    
    public static Date localDateToDate(LocalDate localDate) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        return Date.from(instant);
    }
    
    public static String endDateISO() {
        LocalDate d = LocalDate.now();
        int year = d.getYear();
        int month = d.getMonthValue();
        int day = d.getDayOfMonth();
        LocalDateTime time = LocalDateTime.of(year, month, day, 23, 59);
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'+08:00'");    
        return time.format(df);
    }
    
    public static Date str2DateTime(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat(FULL_DATE_TIME);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(strDate, pos);
        return date;
    }
    
    public static Date str2Date(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat(FULL_DATE);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(strDate, pos);
        return date;
    }
    
    public static String date2Str(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(FULL_DATE);
        String dateString = formatter.format(date);
        return dateString;
    }
    
    public static String date2StrYearMonth(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(YEAR_MONTH);
        String dateString = formatter.format(date);
        return dateString;
    }
    
    public static Date nextDate(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
      //把日期往后增加一天.整数往后推,负数往前移动
        calendar.add(Calendar.DATE,1);
      //这个时间就是日期往后推一天的结果 
        date = calendar.getTime(); 
        return date;
    }
    
    public static boolean isWeekend(LocalDate date){
        DayOfWeek week = date.getDayOfWeek();
        if(week == DayOfWeek.SATURDAY || week == DayOfWeek.SUNDAY){
            return true;
        }  
        return false;  
    }
    
    public static boolean betweenTime(LocalTime open, LocalTime close, LocalTime time){
        if (open.isBefore(time) && close.isAfter(time)) {
            return true;
        } 
        return false;  
    } 
    
    public static void main(String[] args) {
        /*System.out.println(getYearMonth());
        System.out.println(getYear("201907"));
        System.out.println(getMonth("201907"));
        System.out.println(endDateISO());
        System.out.println(date4String());
        String maxCode = "ZQ-CW20191101001";
        int a = maxCode.length() - 3;
        System.out.println(a);
        maxCode = maxCode.substring(maxCode.length() - 3);
        System.out.println(maxCode);
        System.out.println("dfadsfaf" + "-" + null + DateUtils.date4String());
        System.out.println("dfadsfaf" + "-" + "" + DateUtils.date4String());
        System.out.print("ZQ-CW".split("-")[1]);
        float b = 33.33f;
        float c = 23.12f;
        System.out.println(b+c);*/
    	for(int i = 0;i<12;i++) {
    		System.out.println(UUID.randomUUID().toString().replaceAll("-", ""));
    	}
    }
    
    /**
     * timeStamp4String
     * 获取当前时间戳字符串
     * @return
     * String
     */
    public static String timeStamp4String() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(FULL_DATE_TIME);
        LocalDateTime time = LocalDateTime.now();
        String localTime = df.format(time);
        return localTime;
    }
    
    /**
     * timeStamp4String
     * 获取当前日期字符串
     * @return
     * String
     */
    public static String date4String() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(FULLDATE);
        LocalDate time = LocalDate.now();
        String localTime = df.format(time);
        return localTime;
    }
    
    /**
     * timeStamp4String
     * 获取当前日期字符串
     * @return
     * String
     */
    public static String date4String2() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(FULL_DATE);
        LocalDate time = LocalDate.now();
        String localTime = df.format(time);
        return localTime;
    }
    
    /**
     * yyyyMMddHHmmss
     * @return
     */
    public static String timeStamp2String() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(FULLDATETIME);
        LocalDateTime time = LocalDateTime.now();
        String localTime = df.format(time);
        return localTime;
    }
}
